<?php
/**
 * 业主积分查询
 * @author Administrator
 *
 */
class ownerIntegralManage extends CControllerManageBase{
	public function show()
	{
		$model = new ownerModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$pay_status = $this->getRequestData("sel_pay_status","get","-1");
		$card_status = $this->getRequestData("sel_card_status","get","-1");
		$card = $this->getRequestData("txt_card","get");
		$owner_name = $this->getRequestData("txt_owner_name","get");
		$community_name = $this->getRequestData("txt_community_name","get");
		
		$urlParam = array();
		$where = "o.owner_id=os.owner_id and os.shop_id=s.shop_id and o.del_flag=0 and os.del_flag=0";
		
		$cids = $this->getUserCommunityID();
		
		if($community_name)
		{
			//$where .= " and c.community_name='".$community_name."'";
			$cModel = new communityModel();	
				
			$cidList = $cModel->getNameByID($community_name);
			
			if($cidList)
			{
				$cids1 =",".join(",",$cidList);
				
				$cids .= $cids1;
			}
				
			$urlParam["txt_community_name"] = $community_name;
		}
		
		if($cids)
		{
			$where .= " and o.community_id in (".$cids.")";
		}
		
		if($card_status!="-1")
		{
			$where .= " and card_status=".$card_status."";
			
			$urlParam["sel_card_status"] = $card_status;
		}
		
		if($pay_status!="-1")
		{
			$where .= " and pay_status=".$pay_status."";
			
			$urlParam["sel_pay_status"] = $pay_status;
		}
		
		if($card)
		{
			$where .= " and supplementary_card='".$card."'";
			
			$urlParam["txt_card"] = $card;
		}
		
		if($owner_name)
		{
			$where .= " and owner_name='".$owner_name."'";
			
			$urlParam["txt_owner_name"] = $owner_name;
		}
		
		
		
		$this->setViewParam("card_status", $card_status);
		$this->setViewParam("pay_status", $pay_status);
		$this->setViewParam("card", $card);
		$this->setViewParam("owner_name", $owner_name);
		$this->setViewParam("community_name", $community_name);
		
		$cList = array();//$model->selectDataSetByWhere("t_community","community_id,community_name",array(),"del_flag=0");
		
		$this->setViewParam("cList", $cList);
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_card","txt_owner_name","community_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$c = $model->getCountByWhere("","t_owner_shop");
		
		$list = $model->selectPage($pageIndex,$pageSize,
			"o.owner_id,o.user_name,o.owner_name,o.supplementary_card,
			o.card_status,o.first_login,s.shop_id,s.shop_simple_name,s.shop_name,
			os.balance,os.owner_card_status",
		$where,"","",$c,"t_owner o, t_owner_shop os,t_shop s");
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);

		$pageUrl = $pageUrl?"&page=".$pageIndex:"page=".$pageIndex;
		$this->setCssBlock(".container{min-width:1200px;}");
		$this->setMenu("业主积分查询");
		$this->render("ownerIntegral/list",array("list"=>$list,"pageStr"=>$pageStr,"pageUrl"=>$pageUrl));
	}
	
	public function addDetail()
	{
		
		$this->detailOP(1);
	}
	
	public function reduceDetail()
	{
		$this->detailOP(2);
	}
	
	public function cancelDetail()
	{
		$this->detailOP(3);
	}
	
	private function detailOP($type)
	{
		$ownerID = $this->getRequestData("ownerID","GET");
		
		if(!CCommon::checkNum($ownerID))
		{
			$this->sysMsg("参数错误!",1);
		}
		
		$menu = $type==1?"增加":($type==2?"扣减":"撤销");
		
		$this->setMenu(array(array("text"=>"业主积分查询","href"=>$this->_manageDomain."ownerIntegralManage/show/"),array("text"=>"业主".$menu."积分记录")));
		
		$urlParam = array();
		
		$shopModel = new shopModel();
		$shopList = $shopModel->selectDataSetByWhere("","shop_id,shop_simple_name",array(),"del_flag=0");
		
		$s_time=$this->getRequestData("txt_s_time","get",date("Y-m"."-01"));
		$e_time=$this->getRequestData("txt_e_time","get",date("Y-m-d"));
		//$shopID = $this->getRequestData("sel_shop","GET","-1");
		$shopName = $this->getRequestData("txt_shop_name","GET");
		
		$e_time_old = $e_time;
		$e_time = date("Y-m-d",strtotime($e_time." +1 day"));

		$where = "owner_id=".$ownerID." and cost_type=".$type." and cost_time>to_date('".$s_time."','yyyy-mm-dd hh24:mi:ss')";
		$where .= " and cost_time<=to_date('".$e_time."','yyyy-mm-dd hh24:mi:ss')";
		
		$urlParam["ownerID"] = $ownerID;
		$urlParam["txt_s_time"] = $s_time;
		$urlParam["txt_e_time"] = $e_time;
		
		if($shopName)
		{
			$where .= " and s.shop_simple_name='".$shopName."'";
			
			$urlParam["txt_shop_name"] = $shopName;
		}
		
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$icModel = new integralChangeModel();
		
		$pageUrl = $this->getUrlParam($urlParam,array("txt_shop_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$list = $icModel->selectPage($pageIndex,$pageSize,
			"c_id,s_id,pay_id,cost_money,cost_type,cancel_cost_type,cost_time,discount_type,discount_num,owner_card,cost_shop_before_money,cost_shop_after_money,cost_money_t,shop_simple_name,is_cancel",
		"c.shop_id=s.shop_id and ".$where,"c_id desc","",$c,"t_integral_change c, t_shop s");
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);
		
		$this->setJsFile(array("/My97DatePicker/WdatePicker.js"));
		
		$v = $type==1?"addDetail":($type==2?"reduceDetail":"cancelDetail");
		
		$w = $type==1?"1200":($type==2?"1100":"1000");
		
		$this->setCssBlock(".container{min-width:".$w."px; width:100%; margin-right:30px;}");
		$this->render("ownerIntegral/".$v,array("list"=>$list,"s_time"=>$s_time,"owner_id"=>$ownerID,"shop_name"=>$shopName,
			"e_time"=>$e_time_old,"shop_list"=>$shopList,"pageStr"=>$pageStr
		));
	}
}